﻿using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

namespace WisDom.SmartTMS
{
    public class Program
    {
        public static int Main(string[] args)
        {
            Console.WriteLine($@"
                                               
                                                / \
                                               -     ---
                                    - - - = =/        \ \                          
                                             |         \ |\
                 -------      ----------     | .        \ \ \
                |       |    |          |    |    )      / / | \ \
                |  -----     |      ---      \   /  |
                |    |       |         |      ---  /
                |  -----     |      ---
                |       |    |       |
                |       |    |       |
                 -------      -------
            -------------------------------------------------------------------
            Author           :  xingyuxiang
            .Net 5 Repository:  https://gitee.com/xing-y_0/tmswisdomlogisticssystem.git   
            .Net core 3.1    :  https://e.gitee.com/xing-y_0/projects
            -------------------------------------------------------------------
            Start Time:{DateTime.Now:yyyy-MM-dd HH:mm:ss}");

            Log.Logger = new LoggerConfiguration()
#if DEBUG
                .MinimumLevel.Debug()
#else
                .MinimumLevel.Information()
#endif
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .WriteTo.Async(c => c.File("Logs/logs.txt"))
#if DEBUG
                .WriteTo.Async(c => c.Console())
#endif
                .CreateLogger();

            try
            {
                Log.Information("Starting WisDom.SmartTMS.HttpApi.Host.");
                CreateHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly!");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        internal static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(build =>
                {
                    build.AddJsonFile("appsettings.secrets.json", optional: true);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .UseAutofac()
                .UseSerilog();
    }
}
